#!/usr/bin/env python3
"""
调试API响应格式
"""

import requests
import json

def test_simple_api():
    """测试简单API调用"""
    print("测试简单API调用...")
    
    try:
        response = requests.get('http://localhost:5000/statistics/chart_data', timeout=10)
        
        print(f"状态码: {response.status_code}")
        print(f"响应头: {dict(response.headers)}")
        print(f"响应内容: {response.text[:1000]}")
        
        if response.status_code == 200:
            try:
                data = response.json()
                print(f"JSON格式正确")
                print(f"数据键: {list(data.keys()) if isinstance(data, dict) else 'Not a dict'}")
            except json.JSONDecodeError as e:
                print(f"JSON解析错误: {e}")
        
    except Exception as e:
        print(f"请求异常: {e}")

def test_with_config():
    """测试带配置的API调用"""
    print("\n测试带配置的API调用...")
    
    config = {
        "chart_type": "bar",
        "x_field": "symbol",
        "y_field": "count",
        "filters": []
    }
    
    try:
        response = requests.get(
            'http://localhost:5000/statistics/chart_data',
            params={'config': json.dumps(config)},
            timeout=10
        )
        
        print(f"状态码: {response.status_code}")
        print(f"响应内容: {response.text[:1000]}")
        
        if response.status_code == 200:
            try:
                data = response.json()
                print(f"JSON格式正确")
                print(f"数据键: {list(data.keys()) if isinstance(data, dict) else 'Not a dict'}")
                
                if isinstance(data, dict):
                    for key, value in data.items():
                        if isinstance(value, list):
                            print(f"  {key}: 列表，长度={len(value)}")
                        else:
                            print(f"  {key}: {type(value).__name__}")
                            
            except json.JSONDecodeError as e:
                print(f"JSON解析错误: {e}")
        
    except Exception as e:
        print(f"请求异常: {e}")

def test_different_endpoints():
    """测试不同的端点"""
    print("\n测试不同的端点...")
    
    endpoints = [
        'http://localhost:5000/statistics/chart_data',
        'http://localhost:5000/api/chart_data',
        'http://localhost:5000/chart_data'
    ]
    
    for endpoint in endpoints:
        print(f"\n测试端点: {endpoint}")
        try:
            response = requests.get(endpoint, timeout=5)
            print(f"  状态码: {response.status_code}")
            if response.status_code != 404:
                print(f"  响应: {response.text[:200]}...")
        except Exception as e:
            print(f"  异常: {e}")

if __name__ == "__main__":
    test_simple_api()
    test_with_config()
    test_different_endpoints() 